package com.ai.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.web.service.RepairService;
import com.ai.web.vo.RepairV;
import com.ai.web.vo.ResultModel;
import com.tiny.persistence.domain.Repair;
import com.tiny.persistence.repository.RepairRepo;

@Service("repairService")
public class RepairServiceImpl extends BaseServiceImpl<Repair, Integer, RepairV> implements RepairService  {

    @Autowired
    private RepairRepo repairRepo;
    
    @Autowired
    public RepairServiceImpl(RepairRepo repairRepo) {
        super(repairRepo);
    }

    public RepairV getRepair(int userId) throws Exception {
        return getT(userId);
    }

    public ResultModel saveRepair(RepairV repairV) throws Exception {
        
        RepairV saveV = saveT(repairV);
        
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        
        return rm;
    }

    public ResultModel deleteRepairs(List<Integer> userIds) throws Exception {
        deleteTs(userIds);
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("删除成功！");
        
        return rm;
    }
    
    //生成工单
    public ResultModel generateWorkOrders(RepairV repairV) throws Exception {
        RepairV saveV = saveT(repairV);
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        return rm;
    }
    //确认工单
    public ResultModel confirmWorkOrders(RepairV repairV) throws Exception {
        RepairV saveV = saveT(repairV);
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        return rm;
    }
    //反馈工单
    public ResultModel feedbackWorkOrders(RepairV repairV) throws Exception {
        RepairV saveV = saveT(repairV);
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        return rm;
    }
    //设备维护
    public ResultModel equipmentMaintenance(RepairV repairV) throws Exception {
        RepairV saveV = saveT(repairV);
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        return rm;
    }

	public List<Repair> getListByOpenId(String openId) {
		return repairRepo.getListByOpenId(openId);
	}

	public List<Map<String, Object>> getCountByStatus() {
		return repairRepo.getCountByStatus();
	}
	
	public List<Map<String, Object>> getCountByStatus(String repairDate) {
		return repairRepo.getCountByStatus(repairDate);
	}
	
	public List<RepairV> getListByStatus(byte status) throws Exception {
		List<Repair> list = repairRepo.findByStatus(status);
		List<RepairV> viewlist = new ArrayList<RepairV>();
		for(Repair repair : list) {
			RepairV repairV = new RepairV();
			BeanUtils.copyProperties(repair, repairV);
			viewlist.add(repairV);
		}
		return viewlist;
	}
}

